//
//  JYJTwoSegmentCollectionViewCell.m
//  HappyListen
//
//  Created by 冀永金 on 16/9/4.
//  Copyright © 2016年 冀永金. All rights reserved.
//

#import "JYJTwoSegmentCollectionViewCell.h"
#import "BaseTableView.h"
#import "JYJTwoSegmentTableViewCell.h"
#import "JYJSortContentTitleModel.h"
#import "JYJNetWorkParser.h"
#import "JYJTAMacro.h"
#import "JYJTwoSegmentModel.h"
#import "JYJTwoSegmentModelTwo.h"
#import "JYJVCOfFeaturedTopThreeDetails.h"
#import "MJRefresh.h"

@interface JYJTwoSegmentCollectionViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) BaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray <JYJTwoSegmentModel *>*marrOfModle;
@property (nonatomic, strong) NSMutableArray <JYJTwoSegmentModelTwo *>*marrOfHot;
@property (nonatomic, strong) UIImageView *imageOfAnimation;
@property (nonatomic, assign) NSInteger downLoadCount;
@property (nonatomic, strong) NSDictionary *dic;
@property (nonatomic, strong) NSDictionary *dicTwo;
@end
@implementation JYJTwoSegmentCollectionViewCell
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubViews];
        [self createAnimation];
        self.downLoadCount = 1;
        
    }
    return self;
}
-(void)setTitleModel:(JYJSortContentTitleModel *)titleModel {
    _titleModel = titleModel;
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
    NSString *str3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
    
        self.marrOfModle = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        self.dic = result[@"paginated"];
        for (NSDictionary *dic in dataArray) {
            JYJTwoSegmentModel *model = [JYJTwoSegmentModel modelWithDic:dic];
            [self.marrOfModle addObject:model];
        }
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];
    NSString *string1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
    NSString *String = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
    NSString *string3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A1%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    String = [[string1 stringByAppendingString:String]stringByAppendingString:string3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:String andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfHot = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        self.dicTwo = result[@"paginated"];
        for (NSDictionary *dic in dataArray) {
            JYJTwoSegmentModelTwo *model = [JYJTwoSegmentModelTwo modelWithDic:dic];
            [self.marrOfHot addObject:model];
        }
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];

    
}
- (void)createSubViews {
    self.tableView = [[BaseTableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.contentView addSubview:_tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor colorWithRed:0.07 green:0.07 blue:0.08 alpha:1.00];
    [self.tableView registerClass:[JYJTwoSegmentTableViewCell class] forCellReuseIdentifier:@"pool"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MJRefreshGifHeader *header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    // 下拉刷新动画
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 3; i++) {
        NSString *name = [NSString stringWithFormat:@"list_load_%d",i];
        UIImage *image = [UIImage imageNamed:name];
        [marr addObject:image];
    }
    //    /** 普通闲置状态 */
    //    MJRefreshStateIdle = 1,
    //    /** 松开就可以进行刷新的状态 */
    //    MJRefreshStatePulling,
    //    /** 正在刷新中的状态 */
    //    MJRefreshStateRefreshing,
    
    [header setImages:marr forState:MJRefreshStateIdle];
    [header setImages:marr forState:MJRefreshStatePulling];
    [header setImages:marr forState: MJRefreshStateRefreshing];
    self.tableView.mj_header = header;
    header.lastUpdatedTimeLabel.hidden = YES;
    header.stateLabel.hidden = YES;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
- (void)loadNewData {
    NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
    NSString *string = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
    NSString *str3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    string = [[str1 stringByAppendingString:string]stringByAppendingString:str3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfModle = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        for (NSDictionary *dic in dataArray) {
            JYJTwoSegmentModel *model = [JYJTwoSegmentModel modelWithDic:dic];
            [self.marrOfModle addObject:model];
        }
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];
    NSString *string1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
    NSString *String = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
    NSString *string3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A1%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A1%7D%7D";
    String = [[string1 stringByAppendingString:String]stringByAppendingString:string3];
    [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:String andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
        self.marrOfHot = [NSMutableArray array];
        NSArray *dataArray = result[@"data"];
        for (NSDictionary *dic in dataArray) {
            JYJTwoSegmentModelTwo *model = [JYJTwoSegmentModelTwo modelWithDic:dic];
            [self.marrOfHot addObject:model];
        }
        [self.tableView reloadData];
        self.imageOfAnimation.hidden = YES;
    } andFailureBlock:^(NSError *error) {
        
    }];
    

    [self.tableView.mj_header endRefreshing];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.bounds;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.index == 0) {
        return self.marrOfModle.count;
        
    }else {
        return self.marrOfHot.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJTwoSegmentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool" forIndexPath:indexPath];
    if (self.index == 0) {
        JYJTwoSegmentModel *model = self.marrOfModle[indexPath.row];
        [cell setSegmentModel:model];
    }else {
        JYJTwoSegmentModelTwo *model = self.marrOfHot[indexPath.row];
        [cell setSegmentModelTwo:model];
    }
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 110;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    JYJVCOfFeaturedTopThreeDetails *details = [[JYJVCOfFeaturedTopThreeDetails alloc]init];
    if (self.index == 0) {
        JYJTwoSegmentModel *model = self.marrOfModle[indexPath.row];
        details.segmentModel = model;
    }else {
        JYJTwoSegmentModelTwo *model = self.marrOfHot[indexPath.row];
        details.segmentModelTwo = model;
    }
    self.handleAction(details);
}
- (void)createAnimation {
    self.imageOfAnimation = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"main_load_0"]];
    self.imageOfAnimation.frame = CGRectMake(SCREEN_WIDTH / 2 - 30, 150, 80, 200);
    [self addSubview:_imageOfAnimation];
    NSMutableArray *marr = [NSMutableArray array];
    for (int i = 0; i < 5; i++) {
        NSString *imageName = [NSString stringWithFormat:@"main_load_%d",i];
        UIImage *image = [UIImage imageNamed:imageName];
        [marr addObject:image];
    }
    _imageOfAnimation.animationDuration = 1;
    _imageOfAnimation.animationImages  = marr;
    _imageOfAnimation.animationRepeatCount = 0;
    [ _imageOfAnimation startAnimating];
    
    
}
- (void)loadMoreData {
    NSNumber *num = self.dic[@"more"];
    if ([num isEqualToNumber:@1]) {
        self.downLoadCount += 1;
        NSString *str1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
        NSString *string = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
        NSString *str3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A0%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
        NSString *str4 = [NSString stringWithFormat:@"%ld",self.downLoadCount];
        NSString *str5 = @"%7D%7D";
        string = [[[[str1 stringByAppendingString:string]stringByAppendingString:str3] stringByAppendingString:str4] stringByAppendingString:str5];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:string andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJTwoSegmentModel *model = [JYJTwoSegmentModel modelWithDic:dic];
                [self.marrOfModle addObject:model];
            }
            [self.tableView reloadData];
        } andFailureBlock:^(NSError *error) {
            
        }];
        
        [self.tableView.mj_footer endRefreshing];
        
    }
    NSNumber *numTwo = self.dicTwo[@"more"];
    if ([numTwo isEqualToNumber:@1]) {
        self.downLoadCount += 1;
        NSString *string1 = @"json=%7B%22session%22%3A%7B%22uid%22%3A%2233366%22%2C%22sid%22%3A%2216c79d7928f744d894fe9ddf43865f0b%22%7D%2C%22cat_id%22%3A%22";
        NSString *String = [NSString stringWithFormat:@"%ld",self.titleModel.cat_id];
        NSString *string3 = @"%22%2C%22target_id%22%3A%220%22%2C%22keyword%22%3A%22%22%2C%22orderby%22%3A1%2C%22searchtype%22%3A0%2C%22pagination%22%3A%7B%22count%22%3A54%2C%22page%22%3A";
        NSString *string4 = [NSString stringWithFormat:@"%ld",self.downLoadCount];
        NSString *string5 = @"%7D%7D";
        String = [[[[string1 stringByAppendingString:String]stringByAppendingString:string3]stringByAppendingString:string4 ] stringByAppendingString:string5];
        [JYJNetWorkParser POST:@"http://www.aiyinsitanfm.com/mobile/channel/itemlist.jhtml" andBody:String andBodyStyle:JYJBodyString andHeader:HEADDIC andResponse:JYJJSON andSuccessBlock:^(id result) {
            NSArray *dataArray = result[@"data"];
            for (NSDictionary *dic in dataArray) {
                JYJTwoSegmentModelTwo *model = [JYJTwoSegmentModelTwo modelWithDic:dic];
                [self.marrOfHot addObject:model];
            }
            [self.tableView reloadData];
            
        } andFailureBlock:^(NSError *error) {
            
        }];
        [self.tableView.mj_footer endRefreshing];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
